package com.tool4j.controller;

import com.tool4j.entity.params.LoginParams;
import com.tool4j.entity.vo.LoginResponse;
import com.tool4j.entity.vo.UserDetail;
import com.tool4j.service.AuthService;
import com.tool4j.util.http.IPUtil;
import com.tool4j.util.http.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;

/**
 * 登录认证
 *
 * @author Deng.Weiping
 * @since 2023/9/28 10:32
 */
@RestController
@RequestMapping("/auth")
@Slf4j
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private IPUtil ipUtil;

    /**
     * 登录
     *
     * @param params
     * @return
     */
    @PostMapping("/login")
    public Result<LoginResponse> login(ServerHttpRequest request, @RequestBody LoginParams params) {
        String ip = ipUtil.getIp(request);
        params.setIp(ip);
        params.setRegion(ipUtil.searchRegion(ip));
        return Result.ok(authService.login(params));
    }

    /**
     * 注册
     *
     * @param params
     * @return
     */
    @PostMapping("/register")
    public Result<LoginResponse> register(ServerHttpRequest request, @RequestBody LoginParams params) {
        String ip = ipUtil.getIp(request);
        params.setIp(ip);
        params.setRegion(ipUtil.searchRegion(ip));
        return Result.ok(authService.register(params));
    }

    /**
     * 修改密码
     *
     * @param params
     * @return
     */
    @PostMapping("/modifyPwd")
    public Result<Void> modifyPwd(@RequestBody LoginParams params) {
        authService.modifyPwd(params);
        return Result.ok();
    }

    /**
     * 用户信息
     *
     * @return
     */
    @GetMapping("/userInfo")
    public Result<UserDetail> userInfo(ServerWebExchange exchange) {
        return Result.ok(authService.userInfo(exchange));
    }

    /**
     * 发送验证码
     *
     * @param params
     * @return
     */
    @PostMapping("/sendVerifyCode")
    public Result<Boolean> sendVerifyCode(@RequestBody LoginParams params) {
        return Result.ok(authService.sendVerifyCode(params));
    }

}
